<?php
/**
 * @package mikrokosmos
 * mk function classes file created on 20-nov-2004
 * jaco_at_pixeldump_dot_org
 */


/**
 * private:
 * get a valid temporary file name
 */
function get_tmpFileName($seed = "bda"){ return tempnam(SERVER_MKTMP, $seed); }


/**
* private
* get a valid file used to store serialized data
*/
function get_filename_serialized($swfFileName){

	$sfn = substr($swfFileName, 0, strlen($swfFileName) - 4);
	return SERVER_MKTMP .$sfn ."_ser.mks";
}


/**
* private
* get mkConfig paths
* maybe it will soon become obsolete, use SERVER_MK... instead
*/
function get_mkConfig(){

	global $mkConfig;
	return $mkConfig;
}


/**
* private
* get full flash path
* maybe it will soon become obsolete, simply use SERVER_MKFLASH instead
*/
function get_flashPath(){ return SERVER_MKFLASH; }


/**
 * private
 * check if an swf is serialized
 */
function is_serialized($swfFileName){

	$sfPath = get_filename_serialized($swfFileName);
	return file_exists($sfPath);
}



/**
 * private and VERY EXPERIMENTAL
 * output swf on file bearing mk_swf class
 * msc: mk_swf object
 * rb: rawBody data as taken from an existing swf
 */
 function do_swf_output($msc, $rb = OUTPUT_FROM_FRAMELIST_SIMPLE, $saveToFileFlag = true){

 	$swfPath = $msc->get_swfDir() .$msc->get_swfFileName();
 	if(file_exists($swfPath)) unlink($swfPath);

 	$swfStr = $msc->get_signature();
 	$swfStr .= chr($msc->get_swfVersion());
 	$swfStr .= swf_dword(0);			// temporary file size it must be computed just before saving

 	$swfBody = swf_rect(0, 0, $msc->get_stageWidth(), $msc->get_stageHeight());

 	$swfBody .= NIL;
 	$swfBody .= chr($msc->get_fps());
 	$swfBody .= swf_word($msc->get_frameCount());

	$headerTags = $msc->get_headerTags();

	if(count($headerTags)){
		foreach($headerTags as $td) $swfBody .= $td;
	}

 	$bgColStr = $msc->get_backgroundColor();

	if($msc->get_swfVersion() >= 8) {
		$fa = $msc->get_fileAttributes();
		$swfBody .= $fa->update_tagData();
		//if($fa->get_containsMetadata()) $md = $msc->get_metadata();
		//$swfBody .= $md->update_tagData();
	}

 	$r = hexdec(substr($bgColStr, 1, 2));
 	$g = hexdec(substr($bgColStr, 3, 2));
 	$b = hexdec(substr($bgColStr, 5, 2));

 	$swfBody .= swf_setBgcolor($r, $g, $b);

	if($rb == OUTPUT_FROM_FRAMELIST_SIMPLE){

		$frameList = $msc->get_frameList();

 		if(!count($frameList)) {	// at least ONE empty frame must be present in swf!
 			$swfBody .= swf_showFrame();
 		}
 		else {
 	  		foreach($frameList as $tagList){
 				foreach($tagList as $tag) $swfBody .= $tag;
 				$swfBody .= swf_showFrame();
 	  		}
 	  	}
		$swfBody .= SWFTag_end();
 	}
 	else if($rb == OUTPUT_FROM_FRAMELIST_COMPLEX){

 		$mkft = new MKFrameListTranslator();
 		$mkft->set_frameList($msc->get_frameList());
 		$mkft->frameList_to_rawBody();
 		$swfBody .= $mkft->get_rawBody();
 	}
 	else if ($rb == OUTPUT_FROM_RAWBODY){
 		$swfBody .= $msc->get_rawBody();
 	}
 	/*
 	else if ($rb == OUTPUT_FROM_RAWFRAMELIST){

		$rawFrameList = $msc->get_rawFrameList();

  		foreach($rawFrameList as $tagList){

			foreach($tagList as $tag) $swfBody .= $tag;

			$swfBody .= swf_showFrame();
 	  	}

		$swfBody .= SWFTag_end();
 	}
 	*/
 	// ...
 	// ...
 	// ...
 	// ...
 	// ...

	$sfs = swf_dword(strlen($swfBody) + 8);

	$swfStr[4] = $sfs[0];
	$swfStr[5] = $sfs[1];
	$swfStr[6] = $sfs[2];
	$swfStr[7] = $sfs[3];

	if($msc->get_isCompressed() == "yes"){
		$outBody = gzcompress($swfBody, 9);
		$swfStr[0] = "C";	// set compression flag
	}
	else{
		$outBody = $swfBody;
	}

	if($saveToFileFlag){

		$swfout = fopen($swfPath, "wb");

		fwrite($swfout, $swfStr, 8);
		fwrite($swfout, $outBody, strlen($outBody));
		fflush($swfout);
		fclose($swfout);

		$swfOutSize = strlen($outBody) + 8; // amount of data to save
		$sofs = 0;

		do {
			clearstatcache();
			if(file_exists($swfPath)) $sofs = filesize($swfPath);
		} while( $sofs != $swfOutSize);
	}

	return $swfStr .$outBody;
}


/**
 * private
 * compute and returns an associative array contaning these fields:
 * cursor: new seek cursor position
 * offset: nBits offset
 */
function get_readahead_offset($oldOffset, $nBitsSR){
	$rao = array();
	$rao["cursor"] = 1; // cursor move always on 1 byte at least
	$rao["offset"] = 0;

	$nbAhead = $nBitsSR - (8 - $oldOffset);
	$sLength = floor($nbAhead / 8);
	$rao["offset"] = $nbAhead - ($sLength * 8);

	if($rao["offset"] == 8){
		$rao["offset"] = 0;
		$rao["cursor"]++;
	}

	$rao["cursor"] += $sLength;

	return $rao;
}


/**
 * generic and very xperimental:
 * converts an image to a swf
 */
function image2swf($imagePath){

	$ext = substr($imagePath, -3);
	$bitmapType = IMAGE_LOSSLESS;

	if($ext == "jpg") $bitmapType = JPEG2;

	$bs = getimagesize($imagePath);

	$swfFileName = substr(basename($imagePath), 0, -3) ."swf";
	$swfVersion = SWF_DEFAULT_VERSION;
	$stageWidth = $bs[0];
	$stageHeight = $bs[1];
	$fps = SWF_DEFAULT_FPS;
	$frameCount = 1;
	$backgroundColor = SWF_DEFAULT_BACKGROUNDCOLOR;
	$iscompressed = "no";

	$swf = new MKSwf($swfFileName,
					$swfVersion,
					$stageWidth,
					$stageHeight,
					$fps,
					$frameCount,
					$backgroundColor,
					$iscompressed);

	$bitmapID = 1;

	if($ext == "jpg") $bmTag = write_bitmapTag_jpeg($bitmapType, $bitmapID, $imagePath);
	else if($ext == "png") $bmTag = write_bitmapTag_png($bitmapID, $imagePath);

	$shapeID = 2;
	$shapeTag = create_bitmap_shapeTag($shapeID, $bitmapID, $stageWidth, $stageHeight);

	$swf->add_tagData($bmTag);
	$swf->add_tagData($shapeTag);
	$swf->add_tagData(swf_placeObject2($shapeID));
	$swf->swf_output();

	//
	$si = fopen(SERVER_MKTMP .$swfFileName, "rb");
	$strout = fread($si, filesize(SERVER_MKTMP .$swfFileName));
	fclose($si);

    header("Content-type: application/x-shockwave-flash");
	echo $swf->get_swfData();
}


 /**
 * generic and very xperimental:
 * converts an image to binary tag
 */
function image2tag($imagePath, $bitmapID = 1, $x = 0, $y = 0){

	$tagData = "";
	$shapeID = $bitmapID + 1;
	$bm = new BitmapItem($bitmapID);

	$bitmapTag = $bm->create_from_file($imagePath);

	$tagData = swf_word($shapeID);

	/*
	global $swfTagList;
	// in this section we build defineshape tag
	$bmShapeTag = swf_word($shapeID);

	$bmShapeTag .= swf_rect($x, $y, $bitmapWidth, $bitmapHeight); // rect structure for defineshape

	//SHAPEWITHSTYLE
	$bmShapeTag .= chr(0x01); // 1 fillstylecount
	$bmShapeTag .= chr(0x41); // clipped bitmap fill

	$bmShapeTag .= swf_word($bitmapID);

	//	MATRIX
	$bmShapeTag .= chr(0xd9);
	$bmShapeTag .= chr(0x40);
	$bmShapeTag .= chr(0x00);
	$bmShapeTag .= chr(0x05);
	$bmShapeTag .= chr(0x00);
	$bmShapeTag .= chr(0x00);
	$bmShapeTag .= chr(0x00);

	$bmShapeTag .= chr(0x00); // no linestyle array
	$bmShapeTag .= chr(0x10); //

	$bmShapeTag .= swf_shape_record_rect($x, $y, $bitmapWidth, $bitmapHeight);

	$strdsSize = strlen($bmShapeTag);

	$bmShapeHead = SWFTag_long_header($swfTagList["DefineShape"], $strdsSize);

	return $bmShapeHead .$bmShapeTag;
	 */

	return $tagData;
}


?>
